package com.szbber.medical.ui.fragment.setting

import android.graphics.Color
import android.os.Bundle
import android.view.View
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.utils.ToastUtils
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.szbber.commen.base.BaseVMFragment
import com.szbber.commen.bean.NurseLevelListBean
import com.szbber.commen.dialog.DialogUtils
import com.szbber.medical.R
import com.szbber.medical.databinding.FragmentNurselevelSettingBinding
import com.szbber.medical.databinding.ItemNurselevelBinding
import com.szbber.medical.viewmodel.fragment.NurseLevelSettingViewModel
import kotlinx.android.synthetic.main.fragment_nurselevel_setting.*


class NurseLevelSettingFragment :
    BaseVMFragment<FragmentNurselevelSettingBinding, NurseLevelSettingViewModel>(),
    View.OnClickListener {


    companion object {
        fun getInstance(): NurseLevelSettingFragment {
            return NurseLevelSettingFragment()
        }
    }

    override fun getLayoutResId(): Int = R.layout.fragment_nurselevel_setting

    override fun initVM(): NurseLevelSettingViewModel =
        ViewModelProvider(this, ViewModelProvider.NewInstanceFactory()).get(
            NurseLevelSettingViewModel::class.java)

    override fun initView(savedInstanceState: Bundle?) {
        mBinding.nurseLevelSettingModel = mViewModel
        mViewModel.getNurseLevel(activity)
        tvAddCareLabel.setOnClickListener(this)
        observeNurseLevel()
    }

    private fun observeNurseLevel() {
        mViewModel.nurseLevelListBean.observe(this) {
            val adapter = object :
                BaseQuickAdapter<NurseLevelListBean.Data, BaseViewHolder>(R.layout.item_nurselevel,
                    it.data.toMutableList()) {
                override fun convert(holder: BaseViewHolder, item: NurseLevelListBean.Data) {
                    val binding =
                        DataBindingUtil.bind<ViewDataBinding>(holder.itemView) as ItemNurselevelBinding
                    binding.item = item
                    binding.tvColor.setBackgroundColor(Color.parseColor(item.font_color))
                    binding.tvbgColor.setBackgroundColor(Color.parseColor(item.bg_color))
                    binding.tvName.setOnClickListener {
                        DialogUtils.showNurseNameDialog(activity) {
                            mViewModel.getEditNurseLevel(activity,
                                item.id.toString(),
                                it.strValue,
                                item.font_color,
                                item.bg_color)
                        }
                    }
                    binding.tvColor.setOnClickListener {
                        DialogUtils.showNurseColorDialog(activity) {
                            mViewModel.getEditNurseLevel(activity,
                                item.id.toString(),
                                item.level_name,
                                it.strValue,
                                item.bg_color)
                        }
                    }
                    binding.tvbgColor.setOnClickListener {
                        DialogUtils.showNurseColorDialog(activity) {
                            mViewModel.getEditNurseLevel(activity,
                                item.id.toString(),
                                item.level_name,
                                item.font_color,
                                it.strValue)
                        }
                    }
                    binding.tvDelete.setOnClickListener {
                        mViewModel.deleteNurseLevel(activity, item.id.toString())
                    }
                    binding.executePendingBindings()
                }
            }
            val manager = LinearLayoutManager(activity)
            mRecyclerView.adapter = adapter
            mRecyclerView.layoutManager = manager
        }
    }

    override fun onClick(p0: View?) {
        when (p0?.id) {
            R.id.tvAddCareLabel -> {
                DialogUtils.showAddCareLabel(activity) {
                    mViewModel.addNurseLevel(activity, it.addCareLabelBean)
                }
            }
        }
    }

}